package com.sky.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        // 插入套餐表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);
        // 插入套餐菜品关系表
        List<SetmealDish> list=setmealDTO.getSetmealDishes();
        if(list!=null && !list.isEmpty()) {
            list.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.insertBatch(list);
        }
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuerySetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改菜品在售状态
     * 含有停售菜品的套餐不能起售
     * @param status
     * @param id
     */
    @Override
    public void switchSetmealStatus(Integer status, Long id) {
        // 切换起售前判断套餐包含菜品是否都处于起售状态
        if(status==StatusConstant.ENABLE){
            List<Dish> list=setmealDishMapper.selectDishesBySetmealId(id);
            list.forEach(dish -> {
                if(dish.getStatus()==StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据套餐id获取套餐信息：套餐信息+套餐-菜品关系
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO=new SetmealVO();
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> list=setmealDishMapper.selectSetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        List<Long> ids=new ArrayList<>();
        ids.add(setmeal.getId());
        setmealDishMapper.deleteBySetmealIds(ids);
        List<SetmealDish> list=setmealDTO.getSetmealDishes();
        if(list!=null && !list.isEmpty()){
            list.forEach(setmealDish->{
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.insertBatch(list);
        }
    }

    /**
     * 删除套餐,起售中的套餐不能删除
     * @param ids
     */
    @Transactional
    @Override
    public void deleteSetmeal(List<Long> ids) {
        Long onSaleCnt=setmealMapper.cntOnSale(ids);
        if(onSaleCnt>0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.deleteBatch(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 获取在售套餐列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> listSetmealOnSale(Long categoryId) {
        Setmeal setmeal=Setmeal.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return setmealMapper.selectBySetmeal(setmeal);
    }

    @Override
    public List<DishItemVO> getSetmealDishes(Long id) {
        return setmealDishMapper.getSetmealDishes(id);
    }
}
